package bll

import (
	. "7qule.com/sd3/CrossServer/src/bll/challenge/model"
)

type globalChallengeLeaderboardBll struct {
	// 擂台排行榜的信息
	data map[int]*globalChallengeStageSlice
}

var GlobalChallengeLeaderboardBll = &globalChallengeLeaderboardBll{}

// --------------------------------初始化--------------------------------------------------

//
// InitData
// @Description: 初始化数据
// @receiver this
//
func (this *globalChallengeLeaderboardBll) InitData() (errList []error) {
	tempStageMap := GlobalChallengeStageBll.GetData()

	tempMap := make(map[int]*globalChallengeStageSlice)
	for itemKey, item := range tempStageMap {
		tempStageSlice := NewGlobalChallengeStageSlice()
		for i := 1; i <= BaseChallengeLeaderboardLenModelBll.GetData(); i++ {
			if i >= item.GetChallengeStageLen() {
				break
			}
			tempStageSlice.AddChallengeStageLast(item.GetChallengePlayerByRank(i))
		}
		tempMap[itemKey] = tempStageSlice
	}

	this.data = tempMap
	return
}

// --------------------------------生成的基础方法--------------------------------------------------

//
// GetData
// @Description: 获取数据
// @receiver this
// @return map[int]*globalChallengeStageSlice:擂台排行榜数据
//
func (this *globalChallengeLeaderboardBll) GetData() map[int]*globalChallengeStageSlice {
	return this.data
}

// --------------------------------内部方法--------------------------------------------------

//
// getChallengeStageData
// @Description:获取排行榜页签数据
// @receiver this
// @param stageId:页签id
// @return *globalChallengeStageSlice:页签数据
// @return bool:是否存在
//
func (this *globalChallengeLeaderboardBll) getChallengeStageData(stageId int) (*globalChallengeStageSlice, bool) {
	result, exist := this.data[stageId]
	return result, exist
}

// --------------------------------外部方法--------------------------------------------------

//
// ExchangeChallengePlayer
// @Description:交换排行榜排名
// @receiver this
// @param stageId:页签id
// @param playerRank:挑战者排名
// @param challengedRank:被挑战者排名
//
func (this *globalChallengeLeaderboardBll) ExchangeChallengePlayer(stageId, playerRank, challengedRank int) {
	stageData, exist := this.getChallengeStageData(stageId)
	if !exist {
		return
	}

	stageData.ExchangeChallengePlayer(playerRank, challengedRank)
}

//
// UpdatePlayer
// @Description:修改排行榜用户数据
// @receiver this
// @param stageId:页签id
// @param challengedRank:用户排名
// @param playerChallenge:用户数据
//
func (this *globalChallengeLeaderboardBll) UpdatePlayer(stageId, challengedRank int, playerChallenge *GlobalChallengeModel) {
	stageData, exist := this.getChallengeStageData(stageId)
	if !exist {
		return
	}

	stageData.UpdatePlayer(challengedRank, playerChallenge)
}

//
// GetLeaderboardData
// @Description:获取排行榜数据
// @receiver this
// @return map[int]challengeSlice:排行榜数据
//
func (this *globalChallengeLeaderboardBll) GetLeaderboardData() map[int]challengeSlice {
	result := make(map[int]challengeSlice)

	for stageId, stageData := range this.data {
		result[stageId] = stageData.GetData()
	}

	return result
}
